package com.itheima;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * @author 卖报的小二郎@程序员
 * @date 2021/4/27 20:19
 */
public class DemoMain {
    public static void main(String[] args) throws NacosException {
        String dataId="hello.properties";
        String group="demo";
        String serverAddr="127.0.0.1:8848";
        String namespace="604323fb-f712-42fa-98c4-c856f0bba9cc";

        Properties properties=new Properties();
        properties.put("serverAddr",serverAddr);
        properties.put("namespace",namespace);

        //获得配置文件
        ConfigService configService = NacosFactory.createConfigService(properties);
        String config = configService.getConfig(dataId, group, 5000);
        System.out.println(config);

        //监听服务端配置文件的修改
        configService.addListener(dataId, group, new Listener() {
            public Executor getExecutor() {
                return null;
            }

            public void receiveConfigInfo(String configInfo) {
                System.out.println(configInfo);
            }
        });

        while (true){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
